package com.kedacom.freedomLabel.model.bean;

import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.kedacom.freedomLabel.excel.IRowRecord;
import com.kedacom.freedomLabel.model.annotation.ItemCategory;
import com.kedacom.freedomLabel.model.db.Converters;
import com.kedacom.freedomLabel.utils.Cn2Spell;

/**
 * @author licheng
 * @since 2019/11/14 13:32
 */
@Entity
public class Item extends IFirstLetter implements IMainSelectItem, IRowRecord {

    // 索引
    @NonNull
    @PrimaryKey
    public String uuid = Converters.randomUUID();

    // 分组
    @ItemCategory
    @NonNull
    public String category = ItemCategory.ITEM_1;

    // 内容 数据导出时 应保留
    public String value;

    // 缩写（拼音首字母）
    public String shortHand;

    // 编号
    public String number;

    // 备注
    public String remark;

    // 创建时间
    public Long createTime;

    // 更新时间
    public Long updateTime;

    public Item() {
    }

    @Ignore
    public Item(@ItemCategory String category, String value) {
        this.category = category;
        this.value = value;
    }

    @Ignore
    public Item(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return value;
    }

    @Override
    public String getFirstLetter() {
        if (TextUtils.isEmpty(this.value)) {
            return "#";
        }
        String firstLetter = Cn2Spell.getPinYin(this.value).substring(0, 1).toUpperCase();
        if (!firstLetter.matches("[A-Z]")) {
            return "#";
        }
        return firstLetter;
    }

    @Override
    public String getItemName() {
        return this.value;
    }

    @Override
    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (obj instanceof Item) {
            Item other = (Item) obj;
            return TextUtils.equals(this.uuid, other.uuid);
        }
        return false;
    }

    @Override
    public String[] getSelectResultData() {
        return new String[]{this.uuid, this.value};
    }

    @NonNull
    @Override
    public String[] rowValues() {
        return new String[]{number, category, value, remark};
    }

    @Override
    public boolean[] isNumber() {
        return new boolean[0];
    }
}